package leetcode.list;

/**
 * https://leetcode-cn.com/problems/merge-two-sorted-lists/
 *
 * @author mazouri
 * @create 2021-08-18 19:22
 */
public class _21_mergeTwoLists {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode dummyHead = new ListNode(-1);
        ListNode prev = dummyHead;

        while (l1 != null && l2 != null) {
            if (l1.val < l2.val) {
                prev.next = l1;
                l1 = l1.next;
            } else {
                prev.next = l2;
                l2 = l2.next;
            }

            prev = prev.next;
        }

        prev.next = l1 == null ? l2 : l1;

        return dummyHead.next;
    }
}
